// 高阶函数  模拟async
function generatorToAsync(gentratorFn) {
	// return 具有async功能的函数
	return function() {
		// const gen = gentratorFn.apply(this, arguments)
		return new Promise((resolve, reject) => {
			const g = gentratorFn()
			const next1 = g.next() //{ value: Promise { 8 }, done: false }
			next1.value.then(res1 => { // 拿到value
				const next2 = g.next(res1)
				next2.value.then(res2 => {
					const next3 = g.next(res2)
					next3.value.then(res3 => {
						// g.next(res3) //{value:8,done:true}
						resolve(g.next(res3).value)
					})
				})
			})
		})
	}
}

function foo(num) {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
			resolve(num * 2)
		}, 1000)
	})
}

function* gen() {
	// 写死了，不能写死，因为await不限制个数
	const num1 = yield foo(1)
	const num2 = yield foo(num1)
	const num3 = yield foo(num2)
	return num3
}

const asyncFn = generatorToAsync(gen)
// console.log(asyncFn()); //目的结果是一个Promise对象,Promise { <pending> }
asyncFn().then(res => {
	console.log(res);
})


// 官方的async
// async function asyncFn2() {
// 	const num1 = await foo(1)
// 	const num2 = await foo(num1)
// 	const num3 = await foo(num2)
// 	return num3
// }
